package com.wtwd.device.access;

import com.wtwd.device.common.RespCode;
import com.wtwd.device.entity.VerifyResult;
import com.wtwd.device.service.GatewayRedisService;
import com.wtwd.device.utils.SignUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.http.server.reactive.ServerHttpRequest;

import java.util.Map;

/**
 * @author mjy
 * @date 2021/08/18
 * @description
 */
public class WebAccessAdapter extends AccessFactory  {


    public WebAccessAdapter(ServerHttpRequest request) {
        super(request);
    }

    @Override
    public VerifyResult verifyEncrypt(ServerHttpRequest request, GatewayRedisService redisService) {
        VerifyResult verifyResult = null;
        //检验用户token失效,冲突
        if (StringUtils.isBlank(managerToken)){
            verifyResult=new VerifyResult();
            verifyResult.setResult(false);
            verifyResult.setRespCode(RespCode.TOKEN_UNAUTHORIZED);
            return verifyResult;
        }
        //获取token相应的东西
        Map<Object, Object> userMsg = redisService.getUserMsgByToken(managerToken);
        if (userMsg.isEmpty()) {
            verifyResult=new VerifyResult();
            verifyResult.setResult(false);
            verifyResult.setRespCode(RespCode.TOKEN_UNAUTHORIZED);
            return verifyResult;
        }
        String currentToken = (String) userMsg.get("current_token");
        if (!managerToken.equals(currentToken)) {
            verifyResult=new VerifyResult();
            verifyResult.setResult(false);
            verifyResult.setRespCode(RespCode.TOKEN_CONFLICT);
            return verifyResult;
        }
        ////校验加密数据
        String md5 = SignUtil.md5(managerToken + time);
        if (!md5.equalsIgnoreCase(sign)) {
            verifyResult = new VerifyResult();
            verifyResult.setResult(false);
            verifyResult.setRespCode(RespCode.ILLEGAL_OPERATION);
            return verifyResult;
        }
        return null;
    }
}
